package com.twu.biblioteca;

import com.twu.biblioteca.command.*;
import com.twu.biblioteca.model.*;

import java.util.Scanner;

public class BibliotecaApp {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        LibraryController libraryController = new LibraryController();
        addDefaultsBooks(libraryController);
        addDefaultsMovies(libraryController);
        addDefaultUsers(libraryController);

        Invoker invoker = new Invoker();

        invoker.add(new WelcomeCommand(libraryController));
        invoker.execute();

        while(!libraryController.isLogged()){
            System.out.println("Input your Library Number:");
            String libraryNumber = scanner.nextLine();
            System.out.println("Input your password:");
            String password = scanner.nextLine();
            try {
                libraryController.login(libraryNumber,password);
            } catch (BusinessException e) {
                System.out.println(e.getMessage());
            }
        }


        invoker.add(new MainMenuCommand(libraryController));
        invoker.execute();

        while(libraryController.isLogged()){
            System.out.println("Select a option from menu.");
            int opcao = scanner.nextInt();
            Title title;

            switch (opcao){
                case 1:
                    invoker.add(new ListBooksCommand(libraryController));
                    break;
                case 2:
                    invoker.add(new ListMoviesCommand(libraryController));
                    break;
                case 3:
                    System.out.println("Input title id: ");
                    title = libraryController.findTitleById(scanner.nextInt());
                    if(title.isValid()){
                        invoker.add(new CheckoutTitleCommand(libraryController,title));
                    } else {
                        System.out.println("Invalid title.");
                    }
                    break;
                case 4:
                    System.out.println("Input title id: ");
                    title = libraryController.findTitleById(scanner.nextInt());
                    if(title.isValid()){
                        invoker.add(new ReturnTitleCommand(libraryController,title));
                    } else {
                        System.out.println("That is not a valid title to return.");
                    }
                    break;
                case 5:
                    libraryController.logout();
                    break;
                case 6:
                    invoker.add(new ShowUserInformationCommand(libraryController));
                    break;
                case 7:
                    if (libraryController.getLoggedUser().isLibrarian()){
                        invoker.add(new ListTitlesCommand(libraryController));
                    }
                    break;
                default:
                    System.out.println("Select a valid option!");
                    break;
            }
            if (libraryController.isLogged()) {
                invoker.add(new MainMenuCommand(libraryController));
            }
            invoker.execute();

        }

    }

    public static void addDefaultsBooks(LibraryController libraryController){
        libraryController.addTitle(new Book("A Song of Ice and Fire - A Game of Thrones", "George R. R. Martin", 1996));
        libraryController.addTitle(new Book("A Song of Ice and Fire - A Clash of Kings", "George R. R. Martin", 1998));
        libraryController.addTitle(new Book("A Song of Ice and Fire - A Storm of Swords", "George R. R. Martin", 2000));
    }

    public static void addDefaultsMovies(LibraryController libraryController){
        libraryController.addTitle(new Movie("The Lord of the Rings - The Fellowship of the Ring", 2001, "Peter Jackson", 8));
        libraryController.addTitle(new Movie("The Lord of the Rings - The Two Towers", 2002, "Peter Jackson", 8));
        libraryController.addTitle(new Movie("The Lord of the Rings - The Return of the King", 2003, "Peter Jackson", 8));
    }

    public static void addDefaultUsers(LibraryController libraryController){
        User joe = new User("Joe","123-1234","password","joe@gmail.com","+55 81 9999-8888");
        joe.addRole(UserType.CUSTOMER);
        joe.addRole(UserType.LIBRARIAN);
        User bryan = new User("Bryan","123-1235","password","bryan@gmail.com","+55 81 9999-7777");
        bryan.addRole(UserType.CUSTOMER);
        libraryController.addUser(joe);
        libraryController.addUser(bryan);
    }
}
